//
//  LZMusicPlayerManager.m
//  LZMusicPlayer
//
//  Created by wulanzhou on 2016/10/21.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "LZMusicPlayerManager.h"

@interface LZMusicPlayerManager (){

    STKAudioPlayer *_stkAudioPlayer;
}

@end


@implementation LZMusicPlayerManager

@synthesize audioPlayer=_stkAudioPlayer;

+ (LZMusicPlayerManager *)shareInstance{
    static dispatch_once_t pred = 0;
    __strong static LZMusicPlayerManager *_sharedObject = nil;
    dispatch_once(&pred, ^{
        _sharedObject = [[self alloc] init]; // or some other init method
    });
    return _sharedObject;
}

- (instancetype)init{

    if (self=[super init]) {
        
        _stkAudioPlayer = [[STKAudioPlayer alloc] initWithOptions:(STKAudioPlayerOptions){ .flushQueueOnSeek = YES, .enableVolumeMixer = NO, .equalizerBandFrequencies = {50, 100, 200, 400, 800, 1600, 2600, 16000} }];
        _stkAudioPlayer.meteringEnabled = YES;
        _stkAudioPlayer.volume = 1;
    }
    return self;
}

- (NSString *)playTime{
    
    if (_stkAudioPlayer==nil) {
        return @"0:00";
    }
    
    return [self formatTimeFromSeconds:_stkAudioPlayer.progress];
}

- (NSString *)duration{
    if (_stkAudioPlayer==nil) {
        return @"0:00";
    }
    return [self formatTimeFromSeconds:_stkAudioPlayer.duration];
}

- (float)progress{
    if (_stkAudioPlayer==nil) {
        return 0.0;
    }
    return _stkAudioPlayer.progress/_stkAudioPlayer.duration*1.0;
}

#pragma mark -私有方法
-(NSString*) formatTimeFromSeconds:(int)totalSeconds
{
    
    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    //int hours = totalSeconds / 3600;
    
    //return [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];

    return [NSString stringWithFormat:@"%d:%@%d", minutes, seconds>=10?@"":@"0",seconds];
}

@end
